import { Component, OnInit } from '@angular/core';
import { refreshTokenUrl } from "./urlConfig"; // 获取新的访问令牌
import { LocalStorageService } from './service/localStorageService/local-storage.service';
import { HttpService } from './service/httpService/http.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.styl']
})
export class AppComponent implements OnInit {
  newtoken: any = {
    accessToken: '', // 访问令牌
    refreshToken: '',  // 刷新令牌
    role: '', // 权限等级
  }
  constructor(public localStorage: LocalStorageService,
    public http: HttpService,
  ) { }
  ngOnInit() {
    window.setInterval(() => {
      const token = this.localStorage.getItem('token');
      this.http.postData(refreshTokenUrl, { refreshToken: token.refreshToken }).subscribe((event: any) => {
        console.log(event);
        if (event.code === 200) {
          this.newtoken.refreshToken = token.refreshToken;
          this.newtoken.accessToken = event.result.access_token;
          this.newtoken.role = token.role;
          this.localStorage.setItem('token', this.newtoken);
        }
      });

      console.log("刷新token");

    }, 600000);

  }
}
